import Toast from '@vant/weapp/toast/toast';
// 推荐
function getRecommentPetsList(callback, page) {
  if (page) {
    wx.request({
      data: {
        isresource: true,
        page
      },
      url: 'http://localhost:3000/api/pets',
      // url: 'http:///192.168.110.90:3000/api/pets',

      success(res) {
        callback(res)
      }
    })
  } else {
    wx.request({
      url: 'http://localhost:3000/api/pets',
      data: {
        isresource: true,
      },
      success(res) {
        callback(res)
      }
    })
  }
}
// 查找单个宠物
function getPetsDetail(callback, page) {
  if (page) {
    wx.request({
      data: {
        page
      },
      url: 'http://localhost:3000/api/pets',
      // url: 'http:///192.168.110.90:3000/api/pets',

      success(res) {
        callback(res)
      }
    })
  } else {
    wx.request({
      url: 'http://localhost:3000/api/pets',
      // url: 'http:///192.168.110.90:3000/api/pets',

      success(res) {
        callback(res)
      }
    })
  }
}
// 领养
function sendRequest(petID, petName, callback) {
  const user = wx.getStorageSync('user');
  console.log("user", user);
  if (!user) {
    Toast('请先登录');
    wx.navigateTo({
      url: `/pages/login/login`,
    })
  } else if (user.usertype == 'user') {
    Toast('请先认证');
  } else {
    wx.request({
      method: "POST",
      data: {
        userID: user._id,
        petID,
        petName
      },
      url: 'http://localhost:3000/api/addexamine',
      success(res) {
        callback(res)
        wx.showToast({
          title: '已发送请求',
        })
      }
    })
  }
}
// 寻宠列表
function getLostPetsList(callback, page) {
  if (page) {
    wx.request({
      data: {
        page
      },
      url: 'http://localhost:3000/api/petslost',
      // url: 'http:///192.168.110.90:3000/api/petslost',

      
      success(res) {
        callback(res)
      }
    })
  } else {
    wx.request({
      url: 'http://localhost:3000/api/petslost',
      // url: 'http:///192.168.110.90:3000/api/petslost',

      success(res) {
        callback(res)
      }
    })
  }
}
// 发布寻宠
function sendLostRequest(callback, obj) {
  const token = wx.getStorageSync('token');
  console.log(obj);
  const {
    name,
    address,
    time,
    contact,
    files,
    detail,
    price
  } = obj
  if (!token) {
    Toast('请先登录');
    wx.navigateTo({
      url: `/pages/login/login`,
    })
  } else {
    wx.request({
      method: "POST",
      data: {
        name,
        address,
        time,
        contact,
        token,
        files,
        detail,
        price,
      },
      url: 'http://localhost:3000/api/addpetlost',
      success(res) {
        callback()
      }
    })
  }
}
//用户领养列表
function getExamineList(callback, petid) {
  wx.request({
    data: {
      petid
    },
    url: 'http://localhost:3000/api/petexamine',
    success(res) {
      callback(res)
    }
  })
}
// 收藏
function getLike(callback, petID) {
  const token = wx.getStorageSync('token');
  const user = wx.getStorageSync('user');

  if (!token) {
    Toast('请先登录');
    wx.navigateTo({
      url: `/pages/login/login`,
    })
  } else {
    wx.request({
      method: "POST",
      data: {
        userID: user._id,
        petID,
      },
      url: 'http://localhost:3000/api/addlike',
      success(res) {
        wx.showToast({
          title: '已收藏',
        })
      }
    })
  }
}
// 收藏列表
function getLikeList(callback) {
  const user = wx.getStorageSync('user');
  wx.request({
    data: {
      userID: user._id
    },
    url: 'http://localhost:3000/api/like',
    success(res) {
      callback(res)
    }
  })
}
// 取消收藏
function delLike(callback, petID) {
  const user = wx.getStorageSync('user');

  wx.request({
    method: 'POST',
    data: {
      petID,
      userID: user._id
    },
    url: 'http://localhost:3000/api/dellike',
    success(res) {
      callback(res)
      wx.showToast({
        title: '取消收藏',
      })
    }
  })
}

// 我的挂失
function getMyLost(callback) {
  const token = wx.getStorageSync('token');
  // console.log("挂失");
  wx.request({
    data: {
      token
    },
    url: 'http://localhost:3000/api/petslost',
    success(res) {
      callback(res)
    },

  })
}

module.exports = {
  getRecommentPetsList,
  getPetsDetail,
  sendRequest,
  getLostPetsList,
  sendLostRequest,
  getExamineList,
  getLikeList,
  getLike,
  delLike,
  getMyLost
}